package com.qiaolang.dao;

import com.qiaolang.pojo.ID;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by lenovo on 2017/11/12.
 */
@Repository
public class RoleDAO {
    public List selectRole(String role) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ID id=null;
        List list=new ArrayList<>();
        try {
            // 1. 注册驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // 2. 获取数据库的连接
            conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:orcl1", role,role);
            // 3. 执行 SQL
            pstmt = conn.prepareStatement("select * from id");
            rs = pstmt.executeQuery();
            // 4. 显示结果集里面的数据
            list=new ArrayList<>();
            while (rs.next()) {
                id = new ID();
                id.setUsername(rs.getString(1));
                id.setPassword(rs.getString(2));
                list.add(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally { // 5. 释放资源
            try {
                rs.close();
                pstmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}
